package com.ebpm.designer.pvm
{
	import com.ebpm.designer.NodeContainer;
	import com.ebpm.designer.WF;
	import com.ebpm.designer.WP;
	import com.ebpm.designer.item.DownItem;
	import com.ebpm.designer.item.LeftItem;
	import com.ebpm.designer.item.RightItem;
	import com.ebpm.designer.item.TopItem;
	import com.ebpm.designer.line.LineUtil;
	import com.ebpm.designer.mainLayout.variable.Variable;
	
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.TextArea;
	import mx.utils.StringUtil;
	
	import spark.components.Group;
	import spark.primitives.BitmapImage;

	public class UserTask extends Canvas
	{
		
		//图标
		public var titleIcon:Image;
		
		//文本信息
		public var isEditing:Boolean = false;
		public var txtInput:TextArea = new TextArea();
		public var txtLabel:Label = new Label();
		
		//是否切换点击
		public var isAddDirs:Boolean = false;
		
		public var nodeContainer:NodeContainer;
		
		public function UserTask(container:NodeContainer)
		{
			this.x = WF.ITEM_SIZE;
			this.y = WF.ITEM_SIZE;
			this.nodeContainer = container;
			this.initStyle();
			this.initListener();
		}
		
		public function initStyle():void 
		{
			
			this.width = nodeContainer.width - (WF.ITEM_SIZE * 2);
			this.height = nodeContainer.height - (WF.ITEM_SIZE * 2);
			
			this.initIcon();
			this.initTxtInput();
			
			this.setStyle("cornerRadius", 10);
			this.setStyle("borderColor", WF.USERTASK_BORDER_BG);
			this.setStyle("backgroundColor", WF.USERTASK_BG);
			this.setStyle("borderStyle", "solid");
//			this.setStyle("backgroundAlpha", 0.3);
		}
		
		public function initIcon():void
		{
			var titleIcon:Canvas = new Canvas();
			titleIcon.x = WF.USERTASK_IMG_X;
			titleIcon.y = WF.USERTASK_IMG_Y;
			titleIcon.width = WF.USERTASK_IMG_WIDTH;
			titleIcon.height = WF.USERTASK_IMG_HEIGHT;
			
			var group:Group = new Group();
			var bi:BitmapImage = new BitmapImage();
			bi.source = WF.Icon_UserTask;
			bi.width = titleIcon.width;
			bi.height = titleIcon.height;
			group.addElement(bi);
			titleIcon.addElement(group);
			this.addElement(titleIcon);
		}
		
		public function initTxtInput():void {
			this.txtInput.x = 0;
			this.txtInput.y = 0;
			this.txtInput.width = 0;
			this.txtInput.height = 0;
			this.txtLabel.width = this.width - WF.USERTASK_IMG_WIDTH - 16;
			this.txtLabel.height = WF.LINE_TXT_H;
			this.txtLabel.x = WF.ITEM_SIZE + WF.USERTASK_IMG_WIDTH;
			this.txtLabel.y = (this.height - txtLabel.height) / 2;
			this.txtLabel.setStyle("textAlign", "center");
			this.addElement(this.txtInput);
			this.addElement(this.txtLabel);
		}
		
		private function initListener():void {
			this.doubleClickEnabled = true;
			this.addEventListener(MouseEvent.DOUBLE_CLICK, _doubleClick);	
			this.addEventListener(MouseEvent.CLICK, _click);
			this.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDown);
			this.addEventListener(MouseEvent.MOUSE_OVER, _mouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, _mouseOut);
			this.addEventListener(MouseEvent.MOUSE_UP, _mouseUp);
			this.txtInput.addEventListener(KeyboardEvent.KEY_UP, _txtDescKeyUp);
		}
		
		public function _txtDescKeyUp(event:KeyboardEvent):void {
			
			if(event.keyCode == 13) {
				WF.wp.hideVariablePanels();
				closeEditing();
			}
			
		}
		
		public function _click(event:MouseEvent):void {
			toggleDiaplayDirs();
		}
		
		public function _doubleClick(event:MouseEvent):void {
			enableEditing();
		}
		
		/**
		 * 关闭编辑，将textInput内容赋值到 label 上
		 * **/
		public function closeEditing():void {
			
			if(this.isEditing) {
				
				var text:String = this.txtInput.text.replace("\r", "");
				
				txtLabel.width = txtInput.width;
				txtLabel.height = txtInput.height;
				txtLabel.x = txtInput.x;
				txtLabel.y = txtInput.y;
				txtLabel.text = text;
				txtInput.width = 0;
				txtInput.height = 0;
				isEditing = false;
				nodeContainer.variable.desc = text;
				
				WF.wp.userTaskVariablePanel.vc.txtDesc.text = text;
			}
			
		}
		
		public function enableEditing():void {
			
			if(!isEditing) {
				txtInput.width = this.nodeContainer.width - (WF.ITEM_SIZE * 2) - WF.USERTASK_IMG_WIDTH - 16;
				txtInput.height = WF.LINE_TXT_H;
				txtInput.x = WF.ITEM_SIZE + WF.USERTASK_IMG_WIDTH;
				txtInput.y = (this.nodeContainer.height - txtInput.height - WF.ITEM_SIZE * 2) / 2;
				txtInput.text = this.nodeContainer.variable.desc;
				txtLabel.width = 0;
				txtLabel.height = 0;
				isEditing = true;
				txtInput.setFocus();
			}
			
		}
		
		//切换【变化框】
		public function toggleDiaplayDirs():void {
			
			//1.1 - 先删除页面所有【变化框】
			for(var i:int=0; i<WF.wp.nodeContainers.length; i++) {
				var _temp:NodeContainer = WF.wp.nodeContainers.getItemAt(i) as NodeContainer;
				_temp.removeDirs();
			}
			
			//2.1 - 非重复点击【NodeContainer】
			if(WF.wp.lastNodeContaier != WF.wp.nodeContainer) 
			{
				this.nodeContainer.addDirs();
				this.isAddDirs = true;
			} 
				//2.2 - 重复点击【NodeContainer】
			else 
			{
				if(!this.isAddDirs) {
					this.nodeContainer.addDirs();
					this.isAddDirs = true;
				} else {
					this.nodeContainer.removeDirs();
					this.isAddDirs = false;
				}
			}
		}
		
		//修改文本框 和  文本域 的长度、高度、x、y
		public function resizingByParent():void {
			this.width = this.nodeContainer.width - WF.ITEM_SIZE * 2;
			this.height = this.nodeContainer.height - WF.ITEM_SIZE * 2;
			
			if(this.isEditing) {
				txtInput.width = this.nodeContainer.width - (WF.ITEM_SIZE * 2) - WF.USERTASK_IMG_HEIGHT - 16;
				txtInput.height = WF.LINE_TXT_H;
				txtInput.x = WF.ITEM_SIZE + WF.USERTASK_IMG_WIDTH;
				txtInput.y = (this.nodeContainer.height - txtInput.height - WF.ITEM_SIZE * 2) / 2;
			} else {
				txtLabel.width = this.nodeContainer.width - (WF.ITEM_SIZE * 2) - WF.USERTASK_IMG_HEIGHT - 16;
				txtLabel.height = WF.LINE_TXT_H;
				txtLabel.x = WF.ITEM_SIZE + WF.USERTASK_IMG_WIDTH;
				txtLabel.y = (this.nodeContainer.height - txtLabel.height - WF.ITEM_SIZE * 2) / 2;
			}
			
		}
		
		public function _mouseDown(event:MouseEvent):void													
		{
			WF.wp.hideVariablePanels();
			this.displayVariablePanel();
			
			if(WP.isCtrlDown) {
				WP.isDrawingLine = true;
			}
			
			WF.wp.selectLine = null;
			WF.wp.lastNodeContaier = WF.wp.nodeContainer;
			WF.wp.nodeContainer = this.nodeContainer;
			WP.isAllowSelectNodeContainers = false;
			this.nodeContainer.isMoving = true;
			
			this.nodeContainer.startMoveX = this.nodeContainer.x;
			this.nodeContainer.startMoveY = this.nodeContainer.y;
		}
		
		public function displayVariablePanel():void {
			
			//1、将【参数面板】参数清空
			WF.wp.userTaskVariablePanel.vc.clearContent();	
			
			//2、将【参数面板】参数设置为当前NodeContainer的参数
			WF.wp.userTaskVariablePanel.vc.initContent(this.nodeContainer.variable);
			
			//3、显示【参数面板】
			WF.wp.userTaskVariablePanel.showMe();
			
		}
		
		public function _mouseOver(event:MouseEvent):void
		{
			
			//1、页面正在连线，改变背景颜色
			if(WP.isDrawingLine) {
				this.setStyle("backgroundColor", WF.USERTASK_BG_OVER);
			}
			
			//2、隐藏所有【连线点】
			WF.wp.hideNodeContainerItems();
			
			//3、显示当前【连线点】
			this.nodeContainer.displayItems();
		}
		
		public function _mouseOut(event:MouseEvent):void
		{
			
			if(this.nodeContainer.type == "UserTask" || this.nodeContainer.type == "LaneTask") {
				this.setStyle("backgroundColor", WF.USERTASK_BG);
			}
			
			WF.wp.hideNodeContainerItems();
		}
		
		public function _mouseUp(event:MouseEvent):void
		{
			drawLineWithItem();																																						//判断是否是item的连线事件
			drawLineWithCtrl();																																							//判断是否是ctrl的连线事件
			this.setStyle("backgroundColor", WF.USERTASK_BG);																							//将颜色设置为正常背景
		}

		/**
		 * 【item】到【NodeContainer】画线
		 **/
		public function drawLineWithItem():void {
			if(WP.isDrawingLine && !WP.isCtrlDown) {
				var sourceX:Number = WF.wp.lineSourceItem.nodeContainer.x + WF.wp.lineSourceItem.nodeContainer.width;
				var targetX:Number = this.nodeContainer.x + this.nodeContainer.width;
				var sourceY:Number = WF.wp.lineSourceItem.nodeContainer.y + WF.wp.lineSourceItem.nodeContainer.height;
				var targetY:Number = this.nodeContainer.y + this.nodeContainer.height;
				if(WF.wp.lineSourceItem is RightItem) 
				{
					if(Math.abs((sourceX - targetX)) <= this.nodeContainer.width) {
						if(WF.wp.lineSourceItem.nodeContainer.y < this.nodeContainer.y) {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.downItem, this.nodeContainer.topItem);
						} else {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.topItem, this.nodeContainer.downItem);
						}
					}
					else if(this.nodeContainer.x > WF.wp.lineSourceItem.nodeContainer.x) {
						LineUtil.newLine(WF.wp.lineSourceItem, this.nodeContainer.leftItem);
					} else {
						LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.leftItem, this.nodeContainer.rightItem);
					}
				}
				
				else if(WF.wp.lineSourceItem is LeftItem) 
				{
					if(Math.abs((sourceX - targetX)) <= this.nodeContainer.width) {
						if(WF.wp.lineSourceItem.nodeContainer.y < this.nodeContainer.y) {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.downItem, this.nodeContainer.topItem);
						} else {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.topItem, this.nodeContainer.downItem);
						}
					}
					else if(this.nodeContainer.x > WF.wp.lineSourceItem.nodeContainer.x) {
						LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.rightItem, this.nodeContainer.leftItem);
					} else {
						LineUtil.newLine(WF.wp.lineSourceItem, this.nodeContainer.rightItem);
					}
				}
				
				else if(WF.wp.lineSourceItem is TopItem) 
				{
					if(Math.abs((sourceY - targetY)) <= this.nodeContainer.height) {
						if(WF.wp.lineSourceItem.nodeContainer.x < this.nodeContainer.x) {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.rightItem, this.nodeContainer.leftItem);
						} else {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.leftItem, this.nodeContainer.rightItem);
						}
					}
					else if(this.nodeContainer.y > WF.wp.lineSourceItem.nodeContainer.y) {
						LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.downItem, this.nodeContainer.topItem);
					} else {
						LineUtil.newLine(WF.wp.lineSourceItem, this.nodeContainer.downItem);
					}
				}
				
				else if(WF.wp.lineSourceItem is DownItem) 
				{
					if(Math.abs((sourceY - targetY)) <= this.nodeContainer.height) {
						if(WF.wp.lineSourceItem.nodeContainer.x < this.nodeContainer.x) {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.rightItem, this.nodeContainer.leftItem);
						} else {
							LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.leftItem, this.nodeContainer.rightItem);
						}
					}
					else if(this.nodeContainer.y > WF.wp.lineSourceItem.nodeContainer.y) {
						LineUtil.newLine(WF.wp.lineSourceItem, this.nodeContainer.topItem);
					} else {
						LineUtil.newLine(WF.wp.lineSourceItem.nodeContainer.topItem, this.nodeContainer.downItem);
					}
				}
			}
		}
		
		/**
		 * 按住ctrl键画线
		 **/
		public function drawLineWithCtrl():void {
			if(WP.isDrawingLine && WP.isCtrlDown) {
				var sourceNodeContainer:NodeContainer = WF.wp.nodeContainer;
				var sourceX:Number = sourceNodeContainer.x;
				var sourceY:Number = sourceNodeContainer.y;
				var sourceW:Number = sourceNodeContainer.width;
				var sourceH:Number = sourceNodeContainer.height;
				
				if(WF.intersects(sourceX, sourceY, sourceW, sourceH, this.nodeContainer.x, 0, this.nodeContainer.width, this.nodeContainer.y)) {
					LineUtil.newLine(sourceNodeContainer.downItem, this.nodeContainer.topItem);
				}
				else if(WF.intersects(sourceX, sourceY, sourceW, sourceH, this.nodeContainer.x, this.nodeContainer.y + this.nodeContainer.height, this.nodeContainer.width, 2000)) {
					LineUtil.newLine(sourceNodeContainer.topItem, this.nodeContainer.downItem);
				}
				else if(WF.intersects(sourceX, sourceY, sourceW, sourceH, 0, this.nodeContainer.y, this.nodeContainer.x, this.nodeContainer.height)) {
					LineUtil.newLine(sourceNodeContainer.rightItem, this.nodeContainer.leftItem);
				}
				else if(WF.intersects(sourceX, sourceY, sourceW, sourceH, this.nodeContainer.x + this.nodeContainer.width, this.nodeContainer.y, 2000, this.nodeContainer.height)) {
					LineUtil.newLine(sourceNodeContainer.leftItem, this.nodeContainer.rightItem);
				}
				else if(WF.intersects(sourceX, sourceY, sourceW, sourceH, 0, 0, this.nodeContainer.x, this.nodeContainer.y)) {
					LineUtil.newLine(sourceNodeContainer.rightItem, this.nodeContainer.topItem);
				}
				else if(WF.intersects(sourceX, sourceY, sourceW, sourceH, this.nodeContainer.x + this.nodeContainer.width, 0, 2000, this.nodeContainer.y)) {
					LineUtil.newLine(sourceNodeContainer.leftItem, this.nodeContainer.topItem);
				} 
				else if(WF.intersects(sourceX, sourceY, sourceW, sourceH, 0, this.nodeContainer.y + this.nodeContainer.height, this.nodeContainer.x, 2000)) {
					LineUtil.newLine(sourceNodeContainer.rightItem, this.nodeContainer.downItem);
				} 
				else if(WF.intersects(sourceX, sourceY, sourceW, sourceH, this.nodeContainer.x + this.nodeContainer.width, this.nodeContainer.y + this.nodeContainer.height, 2000, 2000)) {
					LineUtil.newLine(sourceNodeContainer.leftItem, this.nodeContainer.downItem);
				}
				
			}
		}
		
		public  function buildTaskXml():Boolean {
			var v:Variable = this.nodeContainer.variable;
			WF.xmlDefinitionsData += "\t<" + this.className;
			if(v.name == null || v.name == "") {
				Alert.show("id号不允许为空");
				return false;
			}
			WF.xmlDefinitionsData += " id=\"" + v.name + "\"";
			if(v.desc != null && v.desc != "") 
				WF.xmlDefinitionsData += " name=\"" + v.desc + "\"";
			if(v.assignee != null && v.assignee != "") 
				WF.xmlDefinitionsData += " assignee=\"" + v.assignee + "\"";
			if(v.user != null && v.user != "") 
				WF.xmlDefinitionsData += " candidateUsers=\"" + v.user + "\"";
			if(v.roler != null && v.roler != "")
				WF.xmlDefinitionsData += " candidateGroups=\"" + v.roler + "\"";
			if(v.org != null && v.org != "")
				WF.xmlDefinitionsData += " candidateOrgs=\"" + v.org + "\"";
			if(v.filter != null && v.filter != "") 
				WF.xmlDefinitionsData += " taskFilter=\"" + v.filter + "\"";
			if(v.listener != null && v.listener != "")
				WF.xmlDefinitionsData += " taskListener=\"" + v.listener + "\"";
			if(v.dueClass != null && v.dueClass != "") 
				WF.xmlDefinitionsData += " serviceTaskExecute=\"" + v.dueClass + "\"";
			if(v.processKey != null && v.processKey != "") {
				WF.xmlDefinitionsData += " subKey=\"" + v.name + "\"";
			}
			if(v.processDesc != null && v.processDesc != "") {
				WF.xmlDefinitionsData += " subDesc=\"" + v.desc + "\"";
			}
			WF.xmlDefinitionsData += " />\n";
			
			WF.xmlPositionData += "\t<" + this.className + " id=\"" + v.name + "\"";
			WF.xmlPositionData += " x=\"" + this.nodeContainer.x + "\" y=\"" + this.nodeContainer.y + "\" w=\"" + this.nodeContainer.width + "\" h=\"" + this.nodeContainer.height + "\"";
			var json:String = "{\"type\":\"" + this.className + "\", \"mission\":\"" + v.name + "\",\"x\":" + this.nodeContainer.x + ",\"y\":" + this.nodeContainer.y + ",\"w\":" + this.nodeContainer.width + ",\"h\":" + this.nodeContainer.height + ", \"desc\":\"" + (v.desc==null?"":v.desc) + "\"}";
			WF.xmlTaskJson += json + ",";
			//WF.xmlPositionData += " json=\"" + json + "\"";
			WF.xmlPositionData += " />\n";
			return true;
		}
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}